package com.demo.passenger.hotel.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.passenger.hotel.entity.HotelInfo;
import com.demo.passenger.hotel.entity.PassengerResidenceInfo;
import com.demo.passenger.hotel.service.IHotelInfoService;
import com.demo.passenger.hotel.service.IPassengerResidenceInfoService;
import com.demo.passenger.hotel.vo.PassengerResidenceInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 旅客入住信息登记表 接口
 * </p>
 *
 * @author chenlw
 * @since 2019-07-20
 */
@RestController
@RequestMapping("/api/passenger/residence/info")
public class PassengerResidenceInfoRestController {
    @Autowired
    IPassengerResidenceInfoService passengerResidenceInfoService;

    @Autowired
    IHotelInfoService hotelInfoService;

    @RequestMapping("/listByParams")
    public Page<PassengerResidenceInfo> listByParams(PassengerResidenceInfo passengerResidenceInfo,
                                                     @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                     @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return passengerResidenceInfoService.listByParams(passengerResidenceInfo, current, size);
    }

    @RequestMapping("/listAll")
    public List<PassengerResidenceInfo> listAll() {
        return passengerResidenceInfoService.list();
    }


    @CrossOrigin(allowCredentials = "true")
    @RequestMapping("/save")
    public Boolean save(PassengerResidenceInfo passengerResidenceInfo) {
        passengerResidenceInfo.setUpdateTime(new Date());
        return passengerResidenceInfoService.saveOrUpdate(passengerResidenceInfo);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Boolean update(PassengerResidenceInfo passengerResidenceInfo) {
        passengerResidenceInfo.setUpdateTime(new Date());
        return passengerResidenceInfoService.saveOrUpdate(passengerResidenceInfo);
    }

    @RequestMapping("/delete")
    public Boolean delete(PassengerResidenceInfo passengerResidenceInfo) {
        return passengerResidenceInfoService.removeById(passengerResidenceInfo.getId());
    }

    @RequestMapping("/read")
    public PassengerResidenceInfo read(PassengerResidenceInfo passengerResidenceInfo) {
        if (StrUtil.isNotEmpty(passengerResidenceInfo.getId())) {
            passengerResidenceInfo = passengerResidenceInfoService.getById(passengerResidenceInfo.getId());
        }
        return passengerResidenceInfo;
    }



    @RequestMapping("/detail")
    public PassengerResidenceInfoVO detail(PassengerResidenceInfo passengerResidenceInfo) {
        PassengerResidenceInfoVO residenceInfoVO = new PassengerResidenceInfoVO();
        if (StrUtil.isNotEmpty(passengerResidenceInfo.getId())) {
            passengerResidenceInfo = passengerResidenceInfoService.getById(passengerResidenceInfo.getId());
            if (passengerResidenceInfo != null) {
                residenceInfoVO.setId(passengerResidenceInfo.getId());
                residenceInfoVO.setStatus(passengerResidenceInfo.getStatus());
                residenceInfoVO.setCreateTime(passengerResidenceInfo.getCreateTime());
                residenceInfoVO.setIsDeleted(passengerResidenceInfo.getIsDeleted());
                residenceInfoVO.setCreateBy(passengerResidenceInfo.getCreateBy());
                // 入住房源
                residenceInfoVO.setHotelId(passengerResidenceInfo.getHotelId());
                // 旅客姓名
                residenceInfoVO.setPassengerName(passengerResidenceInfo.getPassengerName());
                // 入住日期、时间
                residenceInfoVO.setCheckInDate(passengerResidenceInfo.getCheckInDate());
                residenceInfoVO.setCheckInTime(passengerResidenceInfo.getCheckInTime());
                // 退房日期、时间
                residenceInfoVO.setCheckOutDate(passengerResidenceInfo.getCheckOutDate());
                residenceInfoVO.setCheckOutTime(passengerResidenceInfo.getCheckOutTime());
                // 国籍
                residenceInfoVO.setNationality(passengerResidenceInfo.getNationality());
                // 地址
                residenceInfoVO.setAddress(passengerResidenceInfo.getAddress());
                // 职业
                residenceInfoVO.setOccupation(passengerResidenceInfo.getOccupation());
                // 性别
                residenceInfoVO.setSex(passengerResidenceInfo.getSex());
                // 年龄
                residenceInfoVO.setAge(passengerResidenceInfo.getAge());
                // 电话
                residenceInfoVO.setTel(passengerResidenceInfo.getTel());
                // 入住全员姓名
                residenceInfoVO.setAllResidents(passengerResidenceInfo.getAllResidents());
                // 护照图片信息
                residenceInfoVO.setPassportFiles(passengerResidenceInfo.getPassportFiles());
            }
        }
        if (StrUtil.isNotEmpty(passengerResidenceInfo.getHotelId())) {
            HotelInfo hotelInfo = hotelInfoService.getById(passengerResidenceInfo.getHotelId());
            if (hotelInfo != null) {
                // 入住房源信息
                residenceInfoVO.setHotelNo(hotelInfo.getHotelNo());
                residenceInfoVO.setHotelName(hotelInfo.getHotelName());
            }
        }
        return residenceInfoVO;
    }

}
